package LeetCode;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}19:44
 */
public class interview29 {
    public static void main(String[] args) {
        System.out.println(divide(10, -3));
    }
    public static int divide(int dividend, int divisor) {
        if (dividend == 0) {
            return 0;
        }
        if (dividend == Integer.MIN_VALUE && divisor == -1) {
            return Integer.MAX_VALUE;
        }
        //使用牛批的位运算来进行
        boolean natives = (dividend ^ divisor) < 0;
        int result = 0;
        int d = Math.abs(dividend);
        int r = Math.abs(divisor);
        for (int i = 31; i >= 0; i--) {
            if ((d >> i) >= r) {//得到一个适合的i
                result += 1 << i;
                d -= r << i;
            }
        }
        return natives ? -result : result;
    }
}
